package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//定义通知类和通知
//@Component
//@Aspect//设置当前类为AOP切面类
public class MyAdvice {
    /*//@Pointcut 设置切入点方法
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    //@Before: 通知会在切入点方法执行之前执行,设置当前通知方法与切入点之间的绑定方法，
            //当前通知方法在原始切入点方法前运行
    @Before("pt()")
    public void method(){
        System.out.println(System.currentTimeMillis());
    }*/

   /* //AOP通知类型
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    public void pt(){}

    @Pointcut("execution(int com.itheima.dao.BookDao.select())")
    public void pt2(){}*/

   /* //前置通知
    @Before("pt()")
    public void before(){
        System.out.println("before advice ...");
    }

    //后置通知,当前通知方法在原始切入点方法后运行
    @After("pt()")
    public void after(){
        System.out.println("after advice ...");
    }*/

    /*//环绕通知,当前通知方法在原始切入点方法前后运行
    @Around("pt2()")
    public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("around before advice ...");
        //表示对原始操作的调用
        Object ret = pjp.proceed();
        System.out.println("around after advice ...");
        return ret;
    }*/

    /*//返回后通知,当前通知方法在原始切入点方法正常执行完毕后执行
    @AfterReturning("pt2()")
    public void afterReturning(){
        System.out.println("afterReturning advice ...");
    }*/

    //异常后通知,当前通知方法在原始切入点方法异常时执行
   /* public void afterThrowing(){
        System.out.println("afterThrowing advice ...");
    }*/

   /* //AOP通知获取数据
    @Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")
    private void pt(){}*/

   /* @Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println(Arrays.toString(args));
        System.out.println("before advice ..." );
}
    @After("pt()")
    public void after() {
        System.out.println("after advice ...");
    }*/

    /*@Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        Object[] args = pjp.getArgs();
        System.out.println(Arrays.toString(args));
        args[0] = 666;
        Object ret = null;
        try {
            ret = pjp.proceed(args);
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        return ret;
    }*/

   /* @AfterReturning(value = "pt()",returning = "ret")
    public void afterReturning(Object ret) {
        System.out.println("afterReturning advice ..."+ret);
    }*/


  /*  @AfterThrowing(value = "pt()",throwing = "t")
    public void afterThrowing(Throwable t) {
        System.out.println("afterThrowing advice ..."+t);
    }*/
}
